package nxu.it.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import nxu.it.entity.TodayJson;
import nxu.it.entity.User;
import nxu.it.interceptor.LoginFormValidator;
import nxu.it.interceptor.requireLogin;
import nxu.it.service.UserService;

import java.util.ArrayList;
import java.util.List;


@Path(value = "/", viewPath = "/")
public class MainController extends Controller {
    UserService userService = new UserService();


    public void index() {
        renderHtml("<h2>成功!</h2>");
    }

    public void hello() {

        set("username", "Yangliu");
        set("age", 5);

        List list = new ArrayList();
        list.add("杨柳🐏");
        list.add("吴霄🐢");
        set("names", list);
        renderFreeMarker("hello.ftl");

    }

    public void bye() {
        String name = get("name");
        if (name == null || name.length() == 0) {
            name = "无名氏";
        }
        //render返回各种类型
        renderQrCode("再见," + name + "</h2>", 240, 240, 'L');


    }

    @Before(requireLogin.class)
    public void main() {
    }

    @Before(requireLogin.class)
    public void admin() {
    }

    /**
     * 返回今天的日期
     */
    public void today() {
//        使用HashMap
//        Map<String, Object> jsonData = new HashMap<>();
//        使用自带的工具类
//        Kv jsonData = Kv.create().set("year",time);
//        设置属性{year: 2021, month: 4, day: 14, hour: 9, minute: 15, weekday: Wednesday}
//        新建一个类，即可
        TodayJson todayJson = new TodayJson();
        renderJson(todayJson);
    }

    public void register() {
        renderFreeMarker("register.ftl");
    }

    public void loginSuccess() {
        String username = getSessionAttr("username");
        String role = getSessionAttr("role");
        renderHtml("<h2>用户名：" + username + "</h2>" +
                "<h2>角色：" + role + "</h2>"
        );
    }

    public void doLogin() {
        User user = getBean(User.class, "", true);
        System.out.println(user);
        if (userService.checkLogin(user)) {
            setSessionAttr("sno", user.getSno());
            renderHtml("登录成功");
        } else {
            renderHtml("登录失败");
        }

    }



    public void doRegister() {
        User user = getBean(User.class, "", true);
        System.out.println(user);
        if (userService.register(user)) {
            renderHtml("注册成功");
        } else {
            renderHtml("注册失败");
        }

    }

    public void doChapter() {
        renderCaptcha();
    }

    public void login() {
        renderFreeMarker("login.ftl");
    }

    @Before(LoginFormValidator.class)
    public void loginCheck() {
        String username = get("username");
        String password = get("password");
        //验证码校验
        if (validateCaptcha("chapter")) {
            set("message", "验证码错误，请稍后再试！");
            renderFreeMarker("login.ftl");
            return;
        } else {
            setSessionAttr("username", username);
            if (username.equals("admin")) {
                setSessionAttr("role", "admin");
            } else {
                setSessionAttr("role", "user");
            }
            System.out.println();
            //Cookie中放不了复杂的类型
            setCookie("username", username, 30 * 60, true);
            redirect("/loginSuccess");
        }


    }


}
